import java.util.*;
import java.util.function.Function;
import java.util.function.ToDoubleFunction;
import java.util.function.ToIntFunction;
import java.util.function.ToLongFunction;

/*
给定一个二叉搜索树的根节点 root ，和一个整数 k ，请你设计一个算法查找其中第 k 个最小元素（从 1 开始计数）。
 */
  class TreeNode {
      int val;
      TreeNode left;
      TreeNode right;
      TreeNode() {}
      TreeNode(int val) { this.val = val; }
      TreeNode(int val, TreeNode left, TreeNode right) {
          this.val = val;
          this.left = left;
          this.right = right;
      }
      int getVal(){
          return this.val;
      }
  }
class MyComparator implements Comparator<TreeNode>{
    @Override
    public int compare(TreeNode o1, TreeNode o2) {
        return o1.getVal()-o2.getVal();
    }

    @Override
    public Comparator<TreeNode> reversed() {
        return Comparator.super.reversed();
    }

    @Override
    public Comparator<TreeNode> thenComparing(Comparator<? super TreeNode> other) {
        return Comparator.super.thenComparing(other);
    }

    @Override
    public <U> Comparator<TreeNode> thenComparing(Function<? super TreeNode, ? extends U> keyExtractor, Comparator<? super U> keyComparator) {
        return Comparator.super.thenComparing(keyExtractor, keyComparator);
    }

    @Override
    public <U extends Comparable<? super U>> Comparator<TreeNode> thenComparing(Function<? super TreeNode, ? extends U> keyExtractor) {
        return Comparator.super.thenComparing(keyExtractor);
    }

    @Override
    public Comparator<TreeNode> thenComparingInt(ToIntFunction<? super TreeNode> keyExtractor) {
        return Comparator.super.thenComparingInt(keyExtractor);
    }

    @Override
    public Comparator<TreeNode> thenComparingLong(ToLongFunction<? super TreeNode> keyExtractor) {
        return Comparator.super.thenComparingLong(keyExtractor);
    }

    @Override
    public Comparator<TreeNode> thenComparingDouble(ToDoubleFunction<? super TreeNode> keyExtractor) {
        return Comparator.super.thenComparingDouble(keyExtractor);
    }
}
public class KthBinary {
    List<Integer> result=new ArrayList<Integer>();


    public void dfs(TreeNode root){
        if (root!=null)
            result.add(root.val);
        if (root.left!=null)
            dfs(root.left);
        if (root.right!=null)
            dfs(root.right);
    }
    public int kthSmallest(TreeNode root, int k) {
        dfs(root);
       Collections.sort(result);
       return result.get(k-1);
    }

}
